<?php
/*
 * ebiz front
 *
 * GlobalSearchDataDao Model
 *
 * @author allen
 *
 */
require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class GlobalSearchDataDao extends BaseDao
{

    private $tableName = "global_search_data";

    public function __construct($dbUtil){
        parent::__construct($dbUtil);
        $this->_name = $this->tableName;
    }
    
    public function getAllOfSearch($keywords,$cid,$brand,$lprice,$hprice){
    	$sql = "
    	      SELECT pd.id, pd.category_id_1, pd.category_id_2, brand
    	      FROM {$this->tableName} pd  join product_items pi on pi.id = pd.product_item_id and pi.is_delete = 0
    	      join products p on p.id = pd.product_id and p.is_delete = 0
    	      left join product_images pim on pim.item_id = pi.id  and pim.is_delete = 0 and pim.default_item_image=1
    	      WHERE match(key_words) against('$keywords' in boolean mode) and pi.inventory > 0 and pi.sale_price > 0 and pd.is_delete = 0
    	  ";
            if($cid > 0){
    		    $sql .= " and pd.category_id_2 = $cid";
    	    }
    	    if($brand){
    		    $sql .= " and pd.brand = '$brand'";
    	    }
    	    if($lprice){
    	    	$sql .= " and pi.list_price >= $lprice";
    	    }
    	    if ($hprice){
    	    	$sql .= " and pi.list_price <= $hprice";
    	    }
    	 return $this->dbUtil->SelectData($sql,array());
    }
    
    public function getCategoryPNumOfSearch($keywords,$cid,$brand,$lprice,$hprice,$level){
    	if($level == 'brand'){
    		$sql = " SELECT pd.brand, count(pd.brand) as b_num ";
    	} else {
    		$sql = "SELECT pd.category_id_$level as cid, count(pd.category_id_$level) as p_num "; 
    	}
    	 $sql .= "
    	      FROM {$this->tableName} pd  join product_items pi on pi.id = pd.product_item_id and pi.is_delete = 0
    	      join products p on p.id = pd.product_id and p.is_delete = 0
    	      left join product_images pim on pim.item_id = pi.id  and pim.is_delete = 0 and pim.default_item_image=1
    	      WHERE match(key_words) against('$keywords' in boolean mode) and pi.inventory > 0 and pi.sale_price > 0 and pd.is_delete = 0
    	  ";
            if($cid > 0){
    		    $sql .= " and pd.category_id_2 = $cid";
    	    }
    	    if($brand){
    		    $sql .= " and pd.brand = '$brand'";
    	    }
    	    if($lprice){
    	    	$sql .= " and pi.list_price >= $lprice";
    	    }
    	    if ($hprice){
    	    	$sql .= " and pi.list_price <= $hprice";
    	    }
    	    if($level == 'brand'){
    	    	$sql .= " group by brand";
    	    } else {
    	        $sql .= " group by category_id_$level";
    	    }
    	    //print_r($sql);exit;
    	 return $this->dbUtil->SelectData($sql,array());
    }
    
    public function getProductsBySearch($keywords,$int_limit,$int_pagesize,$cid,$brand,$lprice,$hprice){
    	  $sql =  "
    	      SELECT pd.*, pi.id as pi_id,pi.list_price,pi.sale_price,pi.marketing_price,p.title,p.url_title, pim.folder,pim.name  
    	      FROM {$this->tableName} pd  join product_items pi on pi.id = pd.product_item_id and pi.is_delete = 0
    	      join products p on p.id = pd.product_id and p.is_delete = 0
    	      left join product_images pim on pim.item_id = pi.id  and pim.is_delete = 0 and pim.default_item_image=1
    	      WHERE match(key_words) against('$keywords' in boolean mode) and pi.inventory > 0 and pi.sale_price > 0 and pd.is_delete = 0 
    	    ";
    	    if($cid > 0){
    		    $sql .= " and pd.category_id_2 = $cid";
    	    }
    	    if($brand){
    		    $sql .= "  and pd.brand = '$brand'";
    	    }
    	    if($lprice){
    	    	$sql .= " and pi.list_price >= $lprice";
    	    }
    	    if ($hprice){
    	    	$sql .= " and pi.list_price <= $hprice";
    	    }
    	    $sql .= " limit $int_limit, $int_pagesize";
    	
    	    return $this->dbUtil->SelectData($sql,array());
    }

   
}



